package com.example.petcare.controller.user;

import com.example.petcare.pojo.Pet;
import com.example.petcare.pojo.Result;
import com.example.petcare.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController// 标记为控制器类,并且返回json数据
@RequestMapping("/user/pet")// 请求路径
public class PetController {

    @Autowired
    private PetService petService;

    // 新增宠物
    @PostMapping
    public Result addPet(@RequestBody Pet pet) {
        petService.addPet(pet);
        return Result.success();
    }

    // 删除宠物
    @DeleteMapping
    public Result deletePet(@RequestParam("petId") Integer petId) {

        petService.deletePet(petId);
        return Result.success();
    }

    // 修改宠物
    @PutMapping
    public Result updatePet(@RequestBody Pet pet) {
        petService.updatePet(pet);
        return Result.success();
    }

    // 获取宠物详情
    @GetMapping("/detail")
    public Result<Pet> detailPet(Integer petId) {
        Pet pet = petService.findById(petId);
        return Result.success(pet);
    }

    // 获取用户宠物列表
    @GetMapping
    public Result<List<Pet>> listPet() {
        List<Pet> pets = petService.list();
        return Result.success(pets);
    }

}
